#include <Windows.h>
#include "CInventoryItemPacket.h"
#include "CItem.h"
#include "CGeneralItem.h"
#include "CWeaponItem.h"
#include "CArmorItem.h"
#include "CPuppetItem.h"
#include "CUsableItem.h"
#include "CLinkshellItem.h"
#include "CLinkshell.h"

CInventoryItemPacket::CInventoryItemPacket(CItem * item, unsigned char locationID, unsigned char slotID, bool active) {
	this->type = 0x20;
	this->size = 0x16;

	memset(this->data, 0, sizeof(this->data));

	unsigned int quantity = item->getQuantity();
	unsigned short itemID = item->getId();

	this->m_dontProcessOnZone = 0;

	memcpy(this->data+0x04-0x04, &quantity, 4);
	memcpy(this->data+0x0C-0x04, &itemID, 2);
	memcpy(this->data+0x0E-0x04, &locationID, 1);
	memcpy(this->data+0x0F-0x04, &slotID, 1);

	switch(item->getType()) {
		case ITEM_NORMAL:
		case ARMOR_ENCHANTED:
		case WEAPON_ENCHANTED:
		case ARMOR_AUGMENTED:
		case WEAPON_AUGMENTED:
		case ITEM_BIGFISH:
		case ITEM_HOURGLASS:
		case ITEM_BALLISTA:
			break;
		case ITEM_LINKSHELL:
			this->itemLinkshell((CLinkshellItem*)item, active);
			break;
	}
}

CInventoryItemPacket::~CInventoryItemPacket() {

}

void CInventoryItemPacket::itemLinkshell(CLinkshellItem * item, bool active) {
	unsigned char usable = 0x00;
	if(active)
		usable = 0x13;
	unsigned short unknown2 = 0x4ACE;
	//State:
	//00 => New Linkshell
	//01 => Linkshell
	//02 => Pearlsack
	//03 => Linkpearl
	//04 => Ripped pearlsack
	//05 => Broken Linkshell
	//Items 204 & 205 won't display in the ls menue
	//Items 201 - 203 with state >= 4 will display broken instead of the ls name
	unsigned char lsItemState = (item->getId())-0x200;
	unsigned int lsID = item->getLinkshellId();
	if(lsID > 0) {
		CLinkshell * linkshell = CLinkshell::getLinkshellById(item->getLinkshellId());
		if(linkshell != NULL) {
			unsigned short lsColor = linkshell->getLsColor(); 
			char * lsNameComp = linkshell->getLsNameComp();
			memcpy(this->data+0x17-0x04, &lsColor, 2);
			memcpy(this->data+0x1A-0x04, lsNameComp, 16);
		}
	}
	
	unsigned char unknown4 = 0x01;
	memcpy(this->data+0x10-0x04, &usable, 1);
	memcpy(this->data+0x11-0x04, &lsID, 4);
	memcpy(this->data+0x15-0x04, &unknown2, 2);
	memcpy(this->data+0x19-0x04, &lsItemState, 1);

	memcpy(this->data+0x29-0x04, &unknown4, 1);
}